<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
</body>

<script type="text/javascript">
    var date = new Date();
    console.log("date==> ", date);
    console.log("date.toDateString() ==> " , date.toDateString());



    //getMonth() 方法返回 0 -11 ,  getDay()方法返回星期几
    console.log(  "date  =>  yyyy-M-dd : " ,   date.getFullYear() + "-" + date.getMonth() + "-" + date.getDay());
    //这个才是正确的写法
    console.log(  "date  =>  yyyy-M-dd : " ,  date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() );


    //根据字符串设置日期
    var dateStr = '2020-08-20';
    console.log(new Date(dateStr.replace(/-/, "/")));
    console.log(dateStr.split("-"));



    var date0 = new Date(dateStr.split('-'));
    console.log(date0);
    var dateArr = dateStr.split("-");
    console.log(typeof dateArr[2]);
    date0 = new Date(dateArr[0], dateArr[1] - 1, dateArr[2]);
    console.log(date0);

    //根据数字设置日期
    var date1 = new Date();
    date1.setFullYear(2020, 7, 4);
    console.log(date1);

    //两个日期之差的天数
    console.log(parseInt(Math.abs(date0 - date1) / 1000 / 60 / 60 / 24) + 1);

    /* 去掉时分秒 */
    dateStr = "2020-08-06 11:18:13";
    var newDate = /\d{4}-\d{1,2}-\d{1,2}/g.exec(dateStr);
    console.log(newDate);


    /* 分割日期字符串 */
    dateStr = "2021-01-01 - 2021-01-06";
    var dates = dateStr.split(" - ");
    console.log(dates);
    var startDate = new Date(dates[0]);
    var endDate = new Date(dates[1]);

    Date.prototype.diff = function (date) {
        return (this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
    };

    var diff = endDate.diff(startDate);
    console.log(diff);


</script>


</html>
